#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@File    :   try_selenium .py
@Time    :   2024/12/04 18:58:15
@Author  :   xiangyu Chen
@Version :   1.0
@Contact :   yu112yu@sina.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''

# 1. 导包
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time

from selenium.webdriver import ChromeOptions
option = ChromeOptions()

# 打开参数,这个参数是实验性参数
option.add_experimental_option('excludeSwitches', ['enable-automation'])

# 填写chromedriver的目录, 此处使用了相对路径
service = Service('./webdriver/chromedriver_131.0.6778.85_x32.exe')
# 设置浏览器
# chrome_options = Options()
# chrome_options.binary_location = r'E:\HelloWorld\Python\code\learn_selenium\chrome_131.0.6778.85_win32\chrome.exe'  #填写浏览器具体路径
# chrome_options.add_argument('--ignore-certificate-errors')
# chrome_options.add_argument('-ignore -ssl-errors')
# chrome_options.add_argument('--headless')  # 浏览器不提供可视化页面
# chrome_options.add_argument('--disable-gpu')  # 谷歌文档提到需要加上这个属性来规避bug
# # chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 设置开发者模式启动，该模式下webdriver属性为正常值   一般反爬比较好的网址都会根据这个反爬
# chrome_options.add_argument('lang=zh_CN.UTF-8')  # 设置默认编码为 utf-8，也就是中文

# 2. 创建浏览器驱动
driver = webdriver.Chrome(service=service, options=option)
# driver.maximize_window()  #最大化屏幕

# 3. 打开web页面，输入百度
# test_url = "https://book.douban.com/top250"
test_url = "http://local.shop.com/index.php/Home/index/index.html"
driver.get(test_url)

time.sleep(3)  # 休眠3秒
print('driver.current_url: ', driver.current_url)

if (driver.current_url == test_url):
    # 4. 在浏览器地址栏中输入关键字 ：python
    if (driver.current_url == "https://book.douban.com/top250"):
        search_text = driver.find_element(By.ID, "inp-query")
        driver.find_element(By.LINK_TEXT, "inp-query")
        driver.find_elements(By.CSS_SELECTOR, "inp-query")
        # print('search_text :', search_text)
        search_text.clear()
        search_text.send_keys("红楼梦")
        # # 5. 点击按钮搜索图书
        driver.find_element(By.CLASS_NAME, "inp-btn").click()
    if (driver.current_url == "https://www.baidu.com/"):
        search_text = driver.find_element(By.ID, "kw")
        # print('search_text :', search_text)
        search_text.clear()
        search_text.send_keys("红楼梦")
        # # 5. 点击按钮搜索图书
        driver.find_element(By.ID, "su").click()
elif (driver.find_element(By.TAG_NAME, "h1").text == '登录跳转'):
    driver.find_element(By.TAG_NAME, "a").click()

# # 6. 暂停
time.sleep(3)

# 7. 关闭浏览器
driver.quit()
